$(function () {
    //全局定义页数与总条数
    let markid = getUrlString('markid');
    let key = getUrlString('key');
    let type = '1';
    let page = -1,totalSize = 0,sort= 'time',sptypes = '',maintypes = '',maxmoney = '99999',minmoney = '1';
    console.log(key);
    if (key !== '' || key !== null){
        $('#searchVal').val('#'+decodeURIComponent(key)+'#');
        $('.notSearch').hide();
        $('.notSearch,.XDCodeContent').hide();
        $('.indexContent').show();
        $('#pullEggList,#usedList,#ershList,#user').empty();//清空容器;
        page = 0;
        totalSize = 0;
        if (type === '1'){
            $('#usedList,#ershList,#user').hide();
            $('#pullEggList').show();
            pullEggList(page);
        } else if(type === '2'){
            $('#pullEggList,#ershList,#user').hide();
            $('#usedList').show();
            loadpagedata(page)
        }else if(type === '3'){
            $('#usedList,#pullEggList,#user').hide();
            $('#ershList').show();
            ershList(page);
        }else if (type === '4'){
            $('#usedList,#pullEggList,#ershList').hide();
            $('#user').show();
            user(page);
        }
    }

    if (type === '1'){
        $('.indexTab .tabLi').eq(0).addClass('active')
    } else if (type === '2'){
        $('.indexTab .tabLi').eq(1).addClass('active')
    } else if (type === '3'){
        $('.indexTab .tabLi').eq(2).addClass('active')
    }else if (type === '4'){
        $('.indexTab .tabLi').eq(3).addClass('active')
    }

    //清除历史记录
    $('#deleteSearch').click(function () {
        getData('post','/niu/apiv4/niu_delete_search.php',{'markid':markid},function (data) {
            console.log(data);
            if (data.resultcode === '0'){
                $('#searchSpan').empty();
            } else {
                $.toast(data.msg)
            }
        })
    });
    // 历史搜索
    function historySearch (){
        let data = {
            'markid':markid
        };
        getData('post','/niu/apiv3/search_resultlist.php',data,function (data) {
            console.log(data);
            let append = '';
            if (data.data === '' || data.data === null){
                return
            }
            for (let i = 0; i < data.data.length && i < 20;i++){
                append += '<span>'+data.data[i].searchkey+'</span>'
            }

            $('#searchSpan').append(append);

            $('#searchSpan span').click(function () {
                key = $(this).html();
                $('#searchVal').val(key);
                $('.notSearch').hide();
                $('.notSearch,.XDCodeContent').hide();
                $('.indexContent').show();
                $('#pullEggList,#usedList,#ershList,#user').empty();//清空容器;
                page = 0;
                totalSize = 0;
                if (type === '1'){
                    $('#usedList,#ershList,#user').hide();
                    $('#pullEggList').show();
                    pullEggList(page);
                } else if(type === '2'){
                    $('#pullEggList,#ershList,#user').hide();
                    $('#usedList').show();
                    loadpagedata(page)
                }else if(type === '3'){
                    $('#usedList,#pullEggList,#user').hide();
                    $('#ershList').show();
                    ershList(page);
                }else if (type === '4'){
                    $('#usedList,#pullEggList,#ershList').hide();
                    $('#user').show();
                    user(page);
                }
            })
        })
    }

    historySearch();

    // 热门搜索
    function hotSearch (){
        let data = {
            'markid':markid
        };
        getData('post','/niu/apiv4/niu_hot_search.php',data,function (data) {
            console.log(data);
            let append = '';
            for (let i = 0; i < data.data.length && i < 20;i++){
                append += '<span>'+data.data[i].hotkey+'</span>'
            }

            $('#searchHotSpan').append(append);

            $('#searchHotSpan span').click(function () {
                key = $(this).html();
                $('#searchVal').val(key);
                $('.notSearch').hide();
                $('.notSearch,.XDCodeContent').hide();
                $('.indexContent').show();
                $('#pullEggList,#usedList,#ershList,#user').empty();//清空容器;
                page = 0;
                totalSize = 0;
                if (type === '1'){
                    $('#usedList,#ershList,#user').hide();
                    $('#pullEggList').show();
                    pullEggList(page);
                } else if(type === '2'){
                    $('#pullEggList,#ershList,#user').hide();
                    $('#usedList').show();
                    loadpagedata(page)
                }else if(type === '3'){
                    $('#usedList,#pullEggList,#user').hide();
                    $('#ershList').show();
                    ershList(page);
                }else if (type === '4'){
                    $('#usedList,#pullEggList,#ershList').hide();
                    $('#user').show();
                    user(page);
                }
            })
        })
    }

    hotSearch();

    $('#goBack').click(function () {
        nativeCommand("dancool://activity=fin")
    });

    //搜索
    $('#searchBtn').click(function () {
        key = $('#searchVal').val();
        if(key === '' || key === undefined){
            $.toast('搜索内容不能为空');
            $('.indexContent,.nullContent,.XDCodeContent').hide();
            $('.notSearch').show();
            return
        }
        $('.notSearch,.XDCodeContent').hide();
        $('.indexContent').show();
        $('#pullEggList,#usedList,#ershList,#user').empty();//清空容器;
        page = 0;
        totalSize = 0;
        if (type === '1'){
            $('#usedList,#ershList,#user').hide();
            $('#pullEggList').show();
            pullEggList(page);
        } else if(type === '2'){
            $('#pullEggList,#ershList,#user').hide();
            $('#usedList').show();
            loadpagedata(page)
        }else if(type === '3'){
            $('#usedList,#pullEggList,#user').hide();
            $('#ershList').show();
            ershList(page);
        }else if (type === '4'){
            $('#usedList,#pullEggList,#ershList').hide();
            $('#user').show();
            user(page);
        }

    });
    $(".search").keypress(function(e){
        if(e.keyCode === 13) {
            key = $('#searchVal').val();
            if(key === '' || key === undefined){
                $.toast('搜索内容不能为空');
                $('.indexContent,.nullContent,.XDCodeContent').hide();
                $('.notSearch').show();
                return
            }

            $('.notSearch,.XDCodeContent').hide();
            $('.indexContent').show();
            $('#pullEggList,#usedList,#ershList,#user').empty();//清空容器;
            page = 0;
            totalSize = 0;
            if (type === '1'){
                $('#usedList,#ershList,#user').hide();
                $('#pullEggList').show();
                pullEggList(page);
            } else if(type === '2'){
                $('#pullEggList,#ershList,#user').hide();
                $('#usedList').show();
                loadpagedata(page)
            }else if(type === '3'){
                $('#usedList,#pullEggList,#user').hide();
                $('#ershList').show();
                ershList(page);
            }else if (type === '4'){
                $('#usedList,#pullEggList,#ershList').hide();
                $('#user').show();
                user(page);
            }
        }
    });

    //切换搜索
    $('.indexTab .tabLi').click(function () {
        $('.indexTab .tabLi').removeClass('active');
        $(this).addClass('active');
        let index = $(this).index() + 1;
        if (Number(type) !== index){
            type = index.toString();
            $('#pullEggList,#usedList,#ershList,#user').empty();//清空容器;
            page = 0;
            totalSize = 0;
            if (type === '1'){
                $('#usedList,#ershList,#user').hide();
                $('#pullEggList').show();
                pullEggList(page);
            } else if(type === '2'){
                $('#pullEggList,#ershList,#user').hide();
                $('#usedList').show();
                loadpagedata(page)
            }else if(type === '3'){
                $('#usedList,#pullEggList,#user').hide();
                $('#ershList').show();
                ershList(page);
            }else if (type === '4'){
                $('#usedList,#pullEggList,#ershList').hide();
                $('#user').show();
                user(page);
            }
        }
    });


    let mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                $('#usedList,#pullEggList,#ershList,#user').empty();
                totalSize = 0;
                page = -1;
                mRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                if (type === '1'){
                    pullEggList(page)
                } else if (type === '2') {
                    loadpagedata(page)
                }else if (type === '3'){
                    ershList(page)
                }else if (type === '4'){
                    user(page)
                }
            }
        }
    });

    //扯蛋搜索
    function pullEggList(page) {
        if(key === '' || key === undefined){
            return
        }
        let data = {
            'markid':markid,
            'type':'4',
            'key':key,
            'page':page
        };
        getData('get', '/niu/apiv4/niu_weibo_getlistV2.php', data, function (data) {
            console.log(data);
            if(data.resultcode === '0' ){
                let append = '';
                $(data.data).each(function () {
                    let chenghaoStr = (this.userlv).substring(3);

                    append+='<li>' +
                        '           <input type="hidden" class="id" value="'+this.id+'"> '+
                        '                    <div class="communityUser">' +
                        '                       <input type="hidden" class="userId" value="'+this.userid+'">'+
                        '                        <div class="communityUserPic">' +
                        '                            <img src="'+this.userface+'" alt="">' +
                        '                        </div>' +
                        '                        <div class="communityUserName">' +
                        '                            <p>' +
                        '                                <span>'+this.usernick+'</span>' ;
                    if(this.uservip === '1'){
                        append +=  '               <img style="width: 1rem" src="../../res/img/eggTV/monthCard@2x.png" alt="">' ;
                    }
                    if (chenghaoStr < 6 && chenghaoStr > 0){
                        append += '<img src="../../res/img/common/bronzeIcon.png" alt="">' ;
                    }else if (5 < chenghaoStr && chenghaoStr < 11){
                        append += '<img src="../../res/img/common/silverIcon.png" alt="">' ;
                    }else if (10 < chenghaoStr && chenghaoStr < 16){
                        append += '<img src="../../res/img/common/goldIcon.png" alt="">' ;
                    }else if (15 < chenghaoStr && chenghaoStr < 21){
                        append += '<img src="../../res/img/common/platinaIcon.png" alt="">' ;
                    }else if(20 < chenghaoStr && chenghaoStr < 26){
                        append += '<img src="../../res/img/common/diamondIcon.png" alt="">' ;
                    }
                    if(this.chenghao !== ''){
                        append += '<img style="height: 1rem;width: initial" src="https://api.odancool.com'+this.huiyuaninfo2+'" alt="">                  </p>' ;
                    }
                    append+= '                            <p>'+subTime(this.time)+'</p>' +
                        '                        </div>' +
                        '                    </div>' +
                        '                    <div class="title1">'+this.intro+'</div>' ;

                    if(this.picurls !== ''){
                        let picUrls =  this.picurls;
                        let picUrlsArr = picUrls.split(';');

                        append +=  '<div class="infoImg">' ;

                        for (let i = 0;i < picUrlsArr.length;i++){
                            append += '<img src="'+picUrlsArr[i]+'" alt="">' ;
                        }

                        append += '</div>' ;
                    }

                    append += '          <div class="operation">' +
                        '                   <input type="hidden" class="id" value="'+this.id+'"> '+
                        '                        <div class="comment">' +
                        '                            <img src="../../res/img/pullEgg/pl.png" alt="">' +
                        '                            <span>'+this.plcount+'</span>' +
                        '                        </div>' +
                        '                        <div class="like">' +
                        '                            <input type="hidden" class="postid" value="'+this.id+'"> '+
                        '                            <img src="../../res/img/pullEgg/dz.png" alt="">' +
                        '                            <span class="zancount">'+this.zancount+'</span>' +
                        '                        </div>' +
                        '                    </div>' +
                        '                </li>'
                });
                $('#pullEggList').append(append);


                totalSize = totalSize + 10;
                if (totalSize > data.datacount)
                    mRefresh.endUpLoading(true);
                else{
                    mRefresh.resetUpLoading(true);
                    mRefresh.endUpLoading(false);
                }

            }else {
                $.toast(data.msg)
            }

        });
    }

    //打开用户库
    $('#pullEggList li .communityUser').live("click", function(){
        let userId = $(this).find('.userId').val();
        window.location.href = 'dancool://member='+userId;
    });

    //点赞
    $('#pullEggList li .operation .like').live("click", function(){
        let postid = $(this).find('.postid').val();
        let zancount = $(this).find('.zancount').html();
        let _this = $(this).find('.zancount');
        let params = {
            'markid':markid,
            'weiboid':postid
        };
        getData('get', '/niu/apiv3/weibo_dolike.php', params, function (data) {
            console.log(data);
            if(data.resultcode === '0'){
                if(data.command === 'add'){
                    _this.html(numAdd(zancount,1))
                }else {
                    _this.html(numSub(zancount,1))
                }
                $.toast(data.msg)
            }else {
                $.toast(data.msg)
            }
        })
    });

    //去详情
    $('#pullEggList li .title1,#pullEggList li .operation .comment,#pullEggList li .infoImg').live("click", function(){
        let id = $(this).prevAll('.id').val();
        window.location.href = 'dancool://web='+urlView+'pullEgg/pullEggDetail.html?id='+id;
        // window.location.href = 'dancool://weibo='+id;
    });


    //资讯搜索
    function loadpagedata (page) {
        if(key === '' || key === undefined){
            return
        }
        let data = {
            'markid':markid,
            'page':page,
            'key':key,
        };
        getData('post','/niu/apiv4/niu_newslist_apiv4.php',data,function (data) {
            console.log(data);
            if(data.resultcode === '0' ){
                let append = '';
                $(data.data).each(function () {
                    append += '<li>' +
                        '           <input type="hidden" class="id" value="'+this.id+'"> '+
                        '                    <div class="communityUser">' +
                        '                        <div class="communityUserPic">' +
                        '                            <img src="https://img1.odancool.com/defaultface001.jpg-small" alt="">' +
                        '                        </div>' +
                        '                        <div class="communityUserName">' +
                        '                            <p>' +
                        '                                <span>'+this.author+'</span>' +
                        '                            </p>' +
                        '                            <p>'+subTime(this.time)+'</p>' +
                        '                        </div>' +
                        '                    </div>' +
                        '                    <div class="title1">'+this.name+'</div>' +
                        '                    <div class="infoImg">' +
                        '                        <img src="'+this.pic+'" alt="">' +
                        '                    </div>' +
                        '                    <div class="operation">' +
                        '                        <input type="hidden" class="id" value="'+this.id+'"> '+
                        '                        <div class="comment">' +
                        '                            <img src="../../res/img/pullEgg/pl.png" alt="">' +
                        '                            <span>'+this.pingluncount+'</span>' +
                        '                        </div>' +
                        '                        <div class="like">' +
                        '                            <input type="hidden" class="postid" value="'+this.id+'"> '+
                        '                            <img src="../../res/img/pullEgg/dz.png" alt="">' +
                        '                            <span class="zancount">'+this.zancount+'</span>' +
                        '                        </div>' +
                        '                    </div>' +
                        '                </li>'
                });
                $('#usedList').append(append);


                totalSize = totalSize + 10;
                if (totalSize > data.datacount){
                    mRefresh.endUpLoading(true);
                } else{
                    mRefresh.resetUpLoading(true);
                    mRefresh.endUpLoading(false);
                }
            }else {
                $.toast(data.msg)
            }
        });

    }

    //点赞
    $('#usedList li .operation .like').live("click", function(){
        var postid = $(this).find('.postid').val();
        var zancount = $(this).find('.zancount').html();
        var _this = $(this).find('.zancount');
        var params = {
            'markid':markid,
            'postid':postid
        };
        getData('get', '/niu/apiv3/news_dolike.php', params, function (data) {
            console.log(data);
            if(data.resultcode === '0'){
                if(data.command === 'add'){
                    _this.html(numAdd(zancount,1))
                }else {
                    _this.html(numSub(zancount,1))
                }
                laymsg(data.msg)
            }else {
                laymsg(data.msg)
            }
        })
    });

    $('#usedList li .communityUser,#usedList li .title1,#usedList li .operation .comment,#usedList li .infoImg').live("click", function(){
        var id = $(this).prevAll('.id').val();
        window.location.href = 'dancool://news='+id;
    });

    function ershList (page) {
        if(key === '' || key === undefined){
            return
        }
        let params = {
            'key':key,
            'markid':markid,
            'page': page,
        };
        getData('get', '/niu/apiv3/ershou_ershoulist.php', params, function (data) {
            console.log(data);
            if(data.resultcode === '0' ){
                let append = '';
                $(data.data).each(function () {
                    var picUrls =  this.pics;
                    var picUrlsArr = picUrls.split(';');
                    append += '<li>' +
                        '           <input type="hidden" class="id" value="'+this.id+'"> '+
                        '                    <div class="pic">' +
                        '                        <img src="'+picUrlsArr[0]+'" alt="">' +
                        '                    </div>' +
                        '                    <div class="name">'+this.name+'</div>' +
                        '                    <div class="info">' +
                        '                        <span class="infoNum">￥'+this.price+'</span>' +
                        '' +
                        '                        <span class="like">' +
                        '                            <img src="../../res/img/pullEgg/dz1.png" alt="">' +
                        '                            <span class="likeNum">'+this.zancount+'</span>' +
                        '                        </span>' +
                        '                    </div>' +
                        '                    <div class="price">' +
                        '                        <img src="'+this.faceurl+'" alt="">' +
                        '                        <span class="nickName">'+this.nick+'</span>' +
                        '                    </div>' +
                        '                </li>'
                });
                $('#ershList').append(append);
                if (page === 0){
                    mRefresh.scrollTo('5','30');
                }

                totalSize = totalSize + 10;
                if (totalSize > data.datacount){
                    mRefresh.endUpLoading(true);
                } else{
                    mRefresh.resetUpLoading(true);
                    mRefresh.endUpLoading(false);
                }
            }else {
                $.toast(data.msg)
            }
        });
    }
    $("#ershList li").live("click", function(){
        var id = $(this).find('.id').val();
        window.location.href = 'dancool://ershou='+id;
    });

    function user (page) {
        if(key === '' || key === undefined){
            return
        }
        let params = {
            'nick':key,
            'markid':markid,
            'page': page,
        };
        getData('get', '/niu/apiv4/niu_member_getuserlistV2.php', params, function (data) {
            console.log(data);
            if(data.resultcode === '0' ){
                let append = '';
                $(data.data).each(function () {
                    append += '<li>' +
                        '           <input type="hidden" class="id" value="'+this.id+'"> '+
                        '                    <div class="left">' +
                        '                        <img src="'+this.faceurl+'" alt="">' +
                        '                    </div>' +
                        '                    <div class="right">' +
                        '                        <p class="nick">'+this.nick+'</p>' +
                        '                        <p class="userInfo">'+this.set2+'</p>' +
                        '                    </div>' +
                        '                </li>'
                });
                $('#user').append(append);
                if (page === 0){
                    mRefresh.scrollTo('5','30');
                }

                totalSize = totalSize + 10;
                if (totalSize > data.datacount){
                    mRefresh.endUpLoading(true);
                } else{
                    mRefresh.resetUpLoading(true);
                    mRefresh.endUpLoading(false);
                }
            }else {
                $.toast(data.msg)
            }
        });
    }
    $("#user li").live("click", function(){
        let id = $(this).find('.id').val();
        window.location.href = 'dancool://member='+id;
    });
});
